/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.itsv.news.utils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.content.Context;

import com.itsv.news.R;
import com.itsv.news.bean.RssChannelFeed;
import com.itsv.news.bean.RssChannelSub;


/**
 * 
 * @author b456
 *
 */
public class RssXmlParser {
	private static SAXParserFactory spf = SAXParserFactory.newInstance();
	
    /**
     * parser XML document to.
     *
     * @return
     */
    public static RssChannelFeed getRssData(Context context){
        try{
//  		File file = new File(path);
        	InputStream is = null;
        	ChannelParseHandler handler = null;
           	
        	is = context.getResources().openRawResource(R.raw.channel);
            handler = new ChannelParseHandler();
        	
            SAXParser saxParser = spf.newSAXParser();
            XMLReader reader = saxParser.getXMLReader();
            reader.setContentHandler(handler);
            Reader inputReader = new InputStreamReader(is, "GBK");
            InputSource inputSource = new InputSource(inputReader);
            reader.parse(inputSource);
            
            return handler.getRssChannelFeed();              
        }catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    
    /**
     * parser XML document to.
     *
     * @return
     */
    public static RssChannelSub getItemData(Context context, InputStream is){
        try{
        	ItemParseHandler handler = null;
        	
            handler = new ItemParseHandler();
        	
            SAXParser saxParser = spf.newSAXParser();
            XMLReader reader = saxParser.getXMLReader();
            reader.setContentHandler(handler);
            
            Reader inputReader = new InputStreamReader(is, "GBK");
            InputSource inputSource = new InputSource(inputReader);
            reader.parse(inputSource);
            
            return handler.getRssFeed();     
        }catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }    
}
